@extends('wap.layout')
@section('content')
	<!--搜索框-->
	<div class="weui-search-bar" id="searchBar">
	  <form class="weui-search-bar__form"  action="/user_file/search">
	    <div class="weui-search-bar__box">
	      <i class="weui-icon-search"></i>
	      <input type="search" class="weui-search-bar__input" name="search_name" id="searchInput" placeholder="搜索" required="">
		  <input type="hidden" name="folder_id" id="folder_id" />
		  <input type="hidden" name="public_folder_id" id="public_folder_id" />
		  <input type="hidden" name="is_public" id="is_public" />
	      <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
	    </div>
	    <label class="weui-search-bar__label" id="searchText">
	      <i class="weui-icon-search"></i>
	      <span>搜索</span>
	    </label>
	  </form>
	  <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
	</div>
	<!--搜索框end-->
	<!--返回-->
	<div class="gobackBar">
		<a href="javascript:void(0)">返回上一级</a>
	</div>
	<!--返回end-->
	<!--文件列表-->
	<div class="fileList">
		@if(count($results['folder']) > 0 || count($results['file']) > 0)
		<div class="weui-panel weui-panel_access">
		    <!-- body 部分 -->
		    <ul class="weui-panel__bd">
				@foreach ($results['folder'] as $key=>$result)
					<li class="listItem">
						@if($result->is_public != 1)
						<i class="ops"><b></b></i>
						@endif
						<a href="/list?folder_id={{$result->id}}&is_public={{$result->is_public}}" class="weui-media-box weui-media-box_appmsg">
							<div class="weui-media-box__hd">
								<i class="fileIcon floder"></i>
							</div>
							<div class="weui-media-box__bd">
								<h4 class="weui-media-box__title">{{$result->name}}</h4>
								<p class="weui-media-box__desc">{{$result->updated_at}}</p>
							</div>
						</a>
						<div class="opList dis">
							{{--<a href="javascript:;" class="iDownload" id="iDownload{{$result->id}}" data-id="{{$result->id}}">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">下载</p>
							</a>--}}
							@if($result->is_public != 1)
							<a href="javascript:;" class="iDel" id="iDel{{$result->id}}" data-id="{{$result->id}}">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">删除</p>
							</a>
							@endif
						</div>
						<!--更多选项end-->
					</li>
				@endforeach
				@foreach ($results['file'] as $key=>$result)
					@php
						if($result->thumb){
							$is_image = 1;
							$file_icon_html = '<img src="'. $result->thumb .'" alt="" class="weui-media-box__thumb">';
						}else{
							$is_image = 0;
							switch (strtoupper($result->file_ext)){
								case '.DOC':
								case '.DOCX':
									$icon = 'doc';
									break;
								case '.TXT':
									$icon = 'txt';
									break;
								case '.ZIP':
								case '.RAR':
									$icon = 'zip';
									break;
								case '.PPT':
									$icon = 'ppt';
									break;
								case '.XLS':
								case '.XLSX':
									$icon = 'excel';
									break;
								default :
									$icon = 'common';
							}
							$file_icon_html = '<i class="fileIcon '.$icon.'"></i>';
						}
					@endphp
					<li class="listItem">
						<i class="ops"><b></b></i>
						<a href="/detail?user_file_id={{$result->id}}&is_public={{$result->is_public}}" class="weui-media-box weui-media-box_appmsg filetype">
							<div class="weui-media-box__hd">
								{!! $file_icon_html !!}
								{{--<i class="fileIcon {{$icon}}"></i>--}}
							</div>
							<div class="weui-media-box__bd">
								<h4 class="weui-media-box__title">{{$result->name}}</h4>
								<p class="weui-media-box__desc">{{$result->updated_at}}</p>
							</div>
						</a>
						<div class="opList dis">
							<a href="javascript:;" class="iDownload" id="iDownload{{$result->id}}" data-id="{{$result->id}}" data-is_image="{{$is_image}}" data-is_public="{{$result->is_public}}">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">下载</p>
							</a>
							<a href="javascript:;" class="iFav {{$result->collect_status == 1 ? ' ck' : ''}}" id="iFav{{$result->id}}" data-id="{{$result->id}}"  data-is_image="{{$is_image}}">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">收藏</p>
							</a>
							@if($result->is_public != 1)
							<a href="javascript:;" class="iDel" id="iDel{{$result->id}}" data-id="{{$result->id}}">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">删除</p>
							</a>
							@endif
							{{--<a href="/detail?user_file_id={{$result->id}}&is_public={{$result->is_public}}" class="iMore">
								<div class="btnIcon">
									<i></i>
								</div>
								<p class="btnLabel">更多</p>
							</a>--}}
						</div>
						<!--更多选项end-->
					</li>
				@endforeach
		    </ul>
		</div>
		@else
			<div class="pldIcon"><i></i></div>
			<div class="pInfoBar">暂无文件</div>

		@endif
	</div>
	<!--文件列表end-->
	<!--如有文件，则显示-->
	{{--<div class="pInfoBar">298389个文件夹，49857643个文件</div>--}}
	<!--如有文件，则显示end-->
	<div class="placeHolderBar"></div>
<a href="javascript:;" id='show-custom' class="weui-btn weui-btn_primary" style="display:none;">显示自定义对话框</a>
<style>
	.weui-dialog__bd {font-size:18px}
</style>
@stop

@section('ownJs')
<script src="/js/sparkmd5.min.js"></script>
<script>
	var folder_id = getParameterByName('folder_id');
	var public_folder_id = getParameterByName('public_folder_id');
	var is_public = getParameterByName('is_public');
	var upload_time = 0,download_links;
$(function() {
	$('#folder_id').val(folder_id);
	if(public_folder_id != null && public_folder_id != '') $('#public_folder_id').val(public_folder_id);
	if(is_public == null || is_public == ''){
		$('.gobackBar a').attr('href', '/index');
	}else{
		$('#is_public').val(is_public);
		$('.gobackBar a').attr('href', '/list?folder_id='+folder_id+'&is_public='+is_public);
	}
	//文件操作
	$(".fileList").on("click","i.ops",function() {
		$(this).parent("li.listItem").find("i.ops").toggleClass("open");
		$(this).parent("li.listItem").siblings().find(".ops").removeClass("open");
		$(this).parent("li.listItem").siblings().find(".opList").addClass("dis");
		$(this).parent("li.listItem").find(".opList").toggleClass("dis");
	});

	//文件菜单操作
	$(document).on("click", ".iFav", function() {
		var id = $(this).data('id');
		if($(this).hasClass("ck")){
			cancelCollectFile(id);
		}else{
			collectFile(id);
		}
	});

	//删除弹框
	$(document).on("click", ".iDel", function() {
		var id = $(this).data('id');
        $.confirm("您确定要删除文件吗?", "确认删除?", function() {
			deleteFile(id);
        }, function() {
          //取消操作
        });
    });
	//下载文件
	$(document).on("click", ".iDownload", function() {
		var id = $(this).data('id');
		var is_public = $(this).data('is_public');
		download(id, is_public);
	});
	$(document).on("click", "#show-custom", function() {
		$.modal({
			title: "",
			text: download_links,
			buttons: [
				{ text: "复制该链接到浏览器中下载", className: "default"},
//				{ text: "确定", onClick: function(){ $.alert("确定"); } },
			]
		});
	});
});
	//收藏文件
	function collectFile(id){
		$.ajax({
			type: 'get',
			url: ' /collect_file/collect/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('#iFav'+id).toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//取消收藏文件
	function cancelCollectFile(id){
		$.ajax({
			type: 'get',
			url: ' /collect_file/cancel/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('#iFav'+id).toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//删除文件或文件夹
	function deleteFile(id){
		$.ajax({
			type: 'get',
			url: ' /user_file/delete/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					location.href = location.href;
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//下载文件
	function download(id,is_public){
		$.ajax({
			type: 'get',
			url: '/download/auth/'+id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {
				console.log(result.data.access_token);
				if (result.code == 0) {
					var is_image = $('#iDownload'+id).data('is_image');
					if(is_image == '0'){
						var u = navigator.userAgent, app = navigator.appVersion;
						var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
						console.log(u);
						if(isIOS){
							download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}"  +'/download/'+ result.data.access_token;
							$('#show-custom').trigger('click');
						}else{
							location.href = '/download/' + result.data.access_token;
						}
					}else{
						location.href = '/download/' + result.data.access_token;
					}
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						location.href = location.href;
					}, 1000);
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	</script>
@stop
